package com.cms.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.cms.entity.Role;
import com.cms.entity.Tag;
import com.cms.mapper.TagMapper;
import com.cms.utils.DbUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author songfayuan
 * @since 2021-11-12
 */
@Service
public class TagService extends ServiceImpl<TagMapper, Tag> {

    /**
     * 查找所有标签
     *
     * @return 所有标签
     */
    public List<Tag> findAll(){
        QueryWrapper<Tag> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("createDate");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据URL目录查找标签
     *
     * @param cat
     *            URL目录
     * @return 标签，若不存在则返回null
     */
    public Tag findByCat(String cat) {
        if (StringUtils.isBlank(cat)) {
            return null;
        }
        QueryWrapper<Tag> queryWrapper = new QueryWrapper();
        queryWrapper.eq("cat",cat);
        return baseMapper.selectOne(queryWrapper);
    }


    /**
     * 根据名称查找标签
     *
     * @param name
     *            标签名称
     * @return 标签，若不存在则返回null
     */
    public Tag findByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        QueryWrapper<Tag> queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",name);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查找标签分页
     *
     * @param pageNumber
     *            页码
     * @param pageSize
     *            每页记录数
     * @param siteId
     *            站点ID
     * @return 友情链接分页
     */
    public Page<Tag> findPage(Integer pageNumber, Integer pageSize, Integer siteId){
        QueryWrapper<Tag> queryWrapper = new QueryWrapper();
        queryWrapper.eq("siteId",siteId);
        queryWrapper.orderByDesc("createDate");
        return baseMapper.selectPage(new Page<>(pageNumber, pageSize),queryWrapper);
    }

    /**
     * 查找标签
     *
     * @param orderBy
     *            排序
     * @param start
     *            起始位置
     * @param count
     *            数量
     * @return 标签
     */
    public List<Tag> findList(String condition, String orderBy, Integer start, Integer count, Integer siteId){
        String filterSql = " and siteId="+siteId;
        if(StringUtils.isNotBlank(condition)){
            filterSql+=" and "+condition;
        }
        String orderBySql = "";
        if(StringUtils.isNotBlank(orderBy)){
            orderBySql = DbUtils.getOrderBySql(orderBy);
        }else{
            orderBySql = DbUtils.getOrderBySql("createDate desc");
        }
        String countSql= DbUtils.getCountSql(start, count);
        List<Map<String,Object>> tags = SqlRunner.db().selectList("select * from cms_tag where 1=1 "+filterSql+orderBySql+countSql);
        return tags.stream().map(i-> JSON.parseObject(JSON.toJSONString(i), Tag.class)).collect(Collectors.toList());
    }

}
